<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2023/8/9
 * Time: 10:19
 */

namespace app\api\command\spider;
use app\api\command\spider\SpiderBase;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\config;
use think\Db;
use QL\QueryList;

class Zuowen extends SpiderBase
{
    private $baseDomain;
    protected function configure()
    {
        $this->setName('zuowen')              		//定义命令的名字
        ->setDescription('zuowen')      //定义命令的描述
        ->addArgument('method');                   //参数1
    }

    //    php think zuowen spider
    protected function execute(Input $input, Output $output)
    {
        $this->baseDomain = "https://www.99zuowen.com";
        require EXTEND_PATH.'phpQuery/phpQuery.php';
        ini_set('max_execution_time', 600);//秒为单位，自己根据需要定义
        $method = $input->getArgument('method');
        if($method == 'test'){
            ini_set("memory_limit","500M");
            $url = 'https://www.99zuowen.com/xiaoxuezuowen/ynjzuowen/';
            $nj = "一年级";
            $xuexiao = '小学';
            $this->lists($url,$nj,$xuexiao);
//            $url = "https://www.99zuowen.com/xiaoxuezuowen/ynjxieshi/79090.html";
//            $tag = '';
//            $this->details($url,$tag);
        }

    }

    private function lists($url,$nj,$xuexiao){

        $html = _get_url($url);
        $urls = QueryList::html($html)->find('.xbt .right a')->attrs('href');
        $titles = QueryList::html($html)->find('.xbt h3')->texts();
//        print_r($titles[0]);
//        print_r($urls);
//        exit;
        if(count($urls) == count($titles) && !empty($url) && count($urls) > 0){
            foreach($urls as $k=>$v){
                $tagStr = str_replace([$nj,'大全','作文'],['','',''],$titles[$k]);
                $this->listall($v,$tagStr,$nj,$xuexiao);
            }
        }

//        echo $html;
    }

    private function listall($url,$tag,$nj,$xuexiao){
        if(strpos($url,"http") === false){
            $url = "https://www.99zuowen.com".$url;
        }
        echo $nj."=>".$tag."=>".$url."\r\n";
        $html = _get_url($url);
        //获取总共多少页
        $urls = QueryList::html($html)->find('.page ul li a')->attrs('href');
//        print_r($urls);
        $endUrl = $urls[count($urls)-1];
        $pageTemp = explode('.',$endUrl);
        $pageTemp = explode('_',$pageTemp[0]);
        $categoryId = $pageTemp[1];
        $pageMax = $pageTemp[2];
        for($i=1;$i<=$pageMax;$i++){
            $listUrl = $url."list_".$categoryId."_".$i.".html";
            echo "listall=>".$listUrl."\r\n";
            $tags = $nj.",".$tag;
//            echo $tags."\r\n";
            $this->listsone($listUrl,$tags,$nj,$xuexiao);
//            exit;
        }
//        exit;
//        print_r($pageTemp);exit;
//       var_dump($endUrl);
//        exit;
    }

    private function listsone($url,$tags,$nj,$xuexiao){
        echo "listsone:=>".$tags."=>".$url."\r\n";
        $html = _get_url($url);
        $urls = QueryList::html($html)->find('.article ul .lis h4 a')->attrs('href');
//        print_r($urls);exit;
        if(!empty($urls)){
            foreach($urls as $k=>$v){
//                echo $v."\r\n";
                $this->details($v,$tags,$nj,$xuexiao);
            }
//            exit;
        }
    }

    private function details($url,$tags,$xuexiao,$nianji){
        echo "details=>".$url."抓取开始\r\n";
        if($this->_checkSpider($url)){
            //获取type类型开始
            $urlArr = parse_url($url);
            $pathArr = explode('/',$urlArr['path']);
            array_shift($pathArr);
            array_pop($pathArr);
            $pathString = '';
            if(!empty($pathArr)){
                $pathString = implode(',',$pathArr);
            }
            //获取type类型结束
            $html = _get_url($url);
            //获取内容开始
            $contentArr = QueryList::html($html)->find('.content p')->texts();
            $content = '';
            if(!empty($contentArr)){
                foreach($contentArr as $k=>$v){
                    $content .= "<p>".$v."</p>";
                }
            }
//            var_dump($content);exit;
            //标题
            $sourceTitle = QueryList::html($html)->find('h1')->text();
            echo $sourceTitle."\r\n";
            //第一种情况：清晨的雾-随笔作文700字
            $titleArr = explode('-',$sourceTitle);
            $title = $tag = '';
            $zishu = 0;
            if(!empty($titleArr) && count($titleArr) == 2){
                $title = $titleArr[0];
                $zishu = findNum($titleArr[1]);
                $tag = str_replace(['作文','字',$zishu],['','',''],$titleArr[1]);
            } else {
                $titleArr2 = explode('_',$sourceTitle);
                if(!empty($titleArr2) && count($titleArr2) == 2){
                    $title = $titleArr2[0];
                    $zishu = findNum($titleArr2[1]);
                    $tag = str_replace(['作文','字',$zishu],['','',''],$titleArr2[1]);
                } else {
                    $zishu = findNum($sourceTitle);
//                var_dump($zishu);exit;
                    $title = str_replace(['作文','字',$zishu],['','',''],$sourceTitle);
                }




            }
            if(!empty($tag)){
                $tags .=",".$tag;
            }
            echo $title."=>".$zishu."=>".$tags."\r\n";
            $desc = str_cut($content);
            $data = [
                'title'=>$title,
                'content' => $content,
                'source_url' => $url,
                'url_md5' => md5($url),
                'type' => $pathString,
                'weigh' => rand(0,99999999),
                'source_title' => $sourceTitle,
                'summary' => $desc,
                'zishu' => $zishu,
                'nianji' => $nianji,
                'xuexiao' => $xuexiao,
                'tags' => $tags,
            ];
//                            print_r($data);exit;
            $this->insert($data);
//            exit;
        }
    }


    private function insert($data){
//        print_r($data);
        $articleModel = Model('app\common\model\ZuowenSpider');
        $content = $data['content'];
        unset($data['content']);
        $id = $articleModel->insertGetId($data);
        $contentArr = [
            'aid'=>$id,
            'content'=>$content,
        ];
        Model('app\common\model\ZuowenSpiderContent')->insertGetId($contentArr);
    }

    private function _checkSpider($url){
        $articleModel = Model('app\common\model\ZuowenSpider');
//         var_dump($articleModel);exit;
        $articleData = $articleModel->where(['url_md5'=>md5($url)])->find();
        if(empty($articleData)){
            return true;
        } else {
            return false;
        }
        // var_dump($articleData);
    }


}